<!--tvm票款差异协查-->
<template>
  <div class="TvmTicketCYXC el-card is-always-shadow el-card__body">
    <avue-crud
      ref="TvmTicketCYXC"
      :option="option"
      :table-loading="listLoading"
      :data="data"
      :page.sync="page"
      :search.sync="searchForm"
      @search-change="handleFilter"
      @selection-change="selectionChange"
      @refresh-change="getList"
      @on-load="refreshChange">
      <template slot="menuRight">
        <el-button type="primary"  size="mini" @click="submitData">提交</el-button>
        <!-- <el-button type="primary"  size="mini" @click="exportData">导出</el-button> -->
        <el-button type="primary"  size="mini" @click="exportData">导出</el-button>
      </template>
      <template slot-scope="scope" slot="lineCodeSearch">
        <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" clearable @change="changeLine" filterable>
          <el-option
            v-for="(item,index) in lineCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="stationCodeSearch">
        <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" @change="changeStation" filterable>
          <el-option
            v-for="(item,index) in stationCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="tvmOrderSearch">
        <el-select v-model="searchForm.tvmOrder" placeholder="请选择" style="width: 150px" filterable>
            <el-option
              v-for="(item,index) in tvmCodes"
              :key="index"
              :label="item.tvmCode"
              :value="item.tvmCode">
            </el-option>
          </el-select>
      </template>
      <template slot="searchMenu" slot-scope="{row,size}">
        <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm)}" icon="el-icon-delete">清空</el-button>
      </template>
      <template slot-scope="scope" slot="menu">       
        <el-button v-if="scope.row.jdReplyStatus=='0'" type="text" size="small" plain @click.stop="previewEditor(scope.row,2)">编辑</el-button>
        <el-button v-if="scope.row.jdReplyStatus=='1'" type="text" size="small" plain @click.stop="revoke(scope.row)">撤回</el-button>
	<el-button type="text" size="small" plain @click.stop="previewEditor(scope.row,1)">预览</el-button>
      </template>
    </avue-crud>
    <HuikuiInfo ref="HuikuiInfo" v-if="isShowDia"></HuikuiInfo>
</div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import ExportJsonExcel from "js-export-excel";
    import HuikuiInfo from './HuikuiInfo';
    import {
      getgetStationCodes,
      getLineCodes,
      getLineName,
      getQianxiangType,
      getRepalceStatus,
      getStationName,
      getXieChaStatusFormat,
      replayStatus,
      xieChaStatus,
      setSearchFiledsEmpty,
      clearSelect,
      getMappingStatus,
      getSwPmTvmCodeList,
      getMappingStatusArray,
      getStationByCode,
      getLineAndStationByCode,
      getLineAllAsyn,
      fenExYuangettoFixed,
      fenExYuangettoFixed2,
      getAssistCategory,
      assistCategoryDeviceData
    } from '../../../../../../../api/subwayRat/commonCheck';
    import {exportDataToExcel} from "../../../../../../../api/subwayRat/exportSelf";
    import transitUtil from "../../../../../../../api/subwayRat/transitUtil";
    import {
      get_xw_SwAuTvmMoneyAssistListJD,
      submitData_xw_SwAuTvmMoneyAssist,exportTvmMoneyAssist,revokeTvmFenQianXiang
    } from "../../../../../../../api/stationTiecketManager/shuJuXieChaGuanLi/TVM/XT/TVMXTSJCY/JDZYDLCK";
    import { handleExcelResponse } from "@/util/util";
    export default {
      components:{
        HuikuiInfo,
      getLineCodes
      },
      data() {
            return {
              lineCodes:[],
              stationCodes:[],
              tvmCodes:[],
              selectKeys:[], //存放对像
              selectKeyVals:[], //存放值
              isShowDia:false,
              listLoading: true,
              searchForm:{
                lineCode:'9',
                stationCode:'',
                tvmOrder:''
              },
              data: [],
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条,
                isAsc: false// 是否倒序
              },
              option:{},
            }
        },
        created() {
          this.changeLine("9")
          this.option=this.getOption();
        },
        methods: {
          setSearchFiledsEmpty,
          getList() {
            this.listLoading = true;
            Object.assign(this.searchForm,this.page);
            //this.searchForm.assistInvestStatusStr = "2,3,4,5,6,7,8,9";
	    this.searchForm.assistCategoryRole='jd';//查询下发协查类型
            get_xw_SwAuTvmMoneyAssistListJD(this.searchForm).then((response)=>{
              this.data = response.data.data;
              this.listLoading = false;
              this.page.total=response.data.total;
            });
          },
          refreshChange() {
            this.getList()
          },
          selectionChange(list){
            this.selectKeys=list;
            this.selectKeyVals=[];
            for(let i=0;i<list.length;i++){
              this.selectKeyVals.push(list[i].id);
            }
          },
          handleFilter(params, done) {
            Object.assign(this.searchForm,params);
            this.getList()
            done()
          },
          async submitData(){
            if(this.selectKeys.length<=0){
              this.$message.warning("请选择提交的数据！");
              return;
            }

            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].jdReplyStatus==1){
                this.$message.warning("请选择未协查数据！");
                return;
              }
            }
            let messageLSList = [];
            for(let i=0;i<this.selectKeys.length;i++){
              let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
              messageLSList.push({
                "content":"车站：" + data.data[0].name,
                "lineCode":this.selectKeys[i].lineCode,
                "stationCode":this.selectKeys[i].stationCode,
                "replyReason":this.selectKeys[i].replyReason
              })
            }
            let p = {
              messageLSList:JSON.stringify(messageLSList),
              //title:this.$store.getters.tag.label,
	      title:'数据协查管理-TVM层级差异机电协查',
              msgType:"提交",
              type:"JDZYDLCK_001",
              ids:this.selectKeyVals.join(','),
              roleCode: 29
            };
            //下发
            submitData_xw_SwAuTvmMoneyAssist(p).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
              }else{
                this.$message.error("操作失败！");
              }
            })

          },
          changeLine(v){ //改变线路时
            let p={};
            p.line_code=v;
            getStationByCode(p).then(res=>{
              this.stationCodes=res.data;
              this.searchForm.stationCode='';
              this.searchForm.tvmOrder='';
            })
          },
          isassistInvestStatus(){ // 是否协查
            for(let i=0;i<this.selectKeys.length;i++){
              let row=this.selectKeys[i];
              if(row.assistInvestStatus==3){
                return false
              }
            }
            return true;
          },
          getLineName(query){
            let re="";
             getLine(query).then(async res=>{
               re=await res.data.name;
            });
            return re;
          },
          changeStation(v){ //改变车站
            let param={};
            param.lineCode=this.searchForm.lineCode;
            param.stationCode=v;
            getSwPmTvmCodeList(param).then(r=>{
              this.tvmCodes=r.data;
            });
          },
          previewEditor(row,type){ //协查预览
            this.isShowDia=true;
            this.$nextTick(()=>{
              this.$refs.HuikuiInfo.recallShow(row.id,row,type);
            })
          },
          //撤回TVM分钱箱票款差异
          revoke(row){
            let th=this;
            revokeTvmFenQianXiang({ids:row.id}).then(res=>{
              if(res.data.code==0){
                th.$message.success("操作成功！");
                th.getList();
              }else{
                th.$message.error("操作失败！");
              }
            })
          },
          // async exportData(){ // 导出
          //   this.searchForm.pageNo=1;
          //   this.searchForm.pageSize=1000000000;
          //   if(this.selectKeys.length>0){
          //       exportDataToExcel(this.selectKeys,this.option.column,'TVM票款差异协查');
          //   }else {
          //     let res=await get_xw_SwAuTvmMoneyAssistListJD(this.searchForm);
          //     let data = res.data.data;
          //     exportDataToExcel(data,this.option.column,'TVM票款差异协查');
          //   }
          // },
          async exportData(){ //导出
            let loading = this.$loading({
              lock: true,
              text: "Loading",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)"
            })
            try {
              if(this.selectKeys.length>0){
                const res = await exportTvmMoneyAssist({ids:this.selectKeyVals.join(",")});
                handleExcelResponse(res);
              }else{
                this.searchForm.assistInvestStatus = "1";//
                const res = await exportTvmMoneyAssist(this.searchForm);
                handleExcelResponse(res);
              }
            } finally {
              loading.close();
            }
          },
          getOption(){
            let th=this;
            return  {
              emptyBtn:false,
              editBtn:false,
              delBtn:false,
              addBtn:false,
              border: true,
              index:true,
              indexLabel:'序号',
              align:'center',
              menuWidth:150,
              menuAlign:'center',
              selection: true,
              reserveSelection:false,
              height: 450,
              column:[
                {prop:'lineCode',label:'线路',type:'select',search:false,searchslot:true,hide:true,formatter: function(row, value , label, column){
                    return getLineName(value);
                  }},
                {prop:'stationCode',label:'站点',search:true,searchslot:true,formatter: function(row, value , label, column){
                    return getStationName(row.lineCode,value);
                  }},{
                    label: '收入日期',
                    prop: 'settleTime',
                    type: 'date',
                    format: 'yyyy-MM-dd',
                    valueFormat: 'yyyy-MM-dd',
                    width: 90,
                    search:true
                  },
                {prop:'tvmOrder',label:'TVM编号',search:true,searchslot:true},
                {prop:'boxCashType',label:'钱箱类型',width: 120,formatter :function(row, value , label, column){
                    return getQianxiangType(value);
                  },},
                // {prop:'cashBoxCode',label:'钱箱编号'},
                {
                  label: '系统金额',
                  prop: 'systemMoney',
                  formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'systemMoney');
                  }
                },
                {
                  label: '实点金额',
                  prop: 'actualMoney',
                  formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed2(value+row.handOutMoney);
                  }
                },
                {
                  label: '差异金额',
                  prop: 'diffMoney',
                  formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'diffMoney');
                  }
                },
                {prop:'replyReason',label:'机电协查差异原因',type:'select',search:true,searchLabelWidth:"140",width:120,dicData: getMappingStatusArray('JD_REPLY_REASON'),
                  formatter:function(row, value , label, column){
                    return getMappingStatus('JD_REPLY_REASON',value);
                  }},
                {prop:'replyContent',label:'差异详情',hide: true},
                {prop:'sendTime',label:'通知时间',type:'date',search:true,format: 'yyyy-MM-dd',valueFormat:'yyyy-MM-dd',width:90},
                {prop:'operatorName',label:'操作人员'},
                {prop:'operatorCode',label:'操作人员工号',width:100},
                {prop:'jdReplyStatus',label:'协查状态',type:'select',dicData:replayStatus(),search:true,
                  formatter:function(row, value , label, column){
                    return getMappingStatus('jd_reply_status',value);
                  }},
		 {prop:'assistCategory',label:'协查类型',type:'select',dicData:assistCategoryDeviceData(),search:true,
                  formatter:function(row, value , label, column){
                    return getAssistCategory(value);
                  }}
            ]
            }
          },
          otherVueAction(){ //供其他Vue页面调用
            let th=this;
            transitUtil.$on('PK_call',(param)=>{
              get_xw_SwAuTvmMoneyAssistListJD({relationid:param.id}).then((response)=>{
                th.data = response.data.data;
                th.previewEditor(th.data[0]);
              });
            })
            transitUtil.$on('PK_view',(param)=>{
              this.searchForm.relationid = param.id;
              this.getList();
            });

          }
          },
      mounted() {
        // getLineAllAsyn().then(res=>{ //获取线路
        //   this.lineCodes=res.data;
        // });
        getLineCodes().then(res=>{ //获取线路
          this.lineCodes=res;
        });
        this.otherVueAction();
      }
    }
</script>
<style lang="scss">

</style>
